package gov.mmgt.netmanager.ui;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import gov.mmgt.netmanager.common.exception.MyException;
import gov.mmgt.netmanager.service.AccountService;
import gov.mmgt.netmanager.ui.common.MyDialog;
import gov.mmgt.netmanager.vo.AccountVO;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Combo;

/**
 * 
 * Copyright (c) 2013,茂名市国土资源局<br>
 * All rights reserved.<br>
 * 
 * 文件名称：UserManagerPanle.java<br>
 * 摘    要：用户管理面板类<br>
 * <br>
 * 当前版本：<br>
 * 作    者：梁铭显 Email: <a href="mailto:liangmingxian@gmail.com">liangmingxian@gmail.com</a><br>
 * 完成日期：2014-5-26<br>
 * <br>
 * 取代版本：<br>
 * 原作者  ：<br>
 * 完成日期：2014-5-26<br>
 */
public class UserManagerPanle extends Composite {
	private Composite panle;
	private int id;
	private Text account;
	private Text password;
	private Combo isAdmin;
	private Button addUserBtn;
	private Table table;
	private TableColumn tableColumn_1;
	private TableColumn tableColumn_2;
	private TableColumn tableColumn_3;
	private Text name;
	private CLabel adminLabel;
	
	private boolean isAddUser;
	private AccountService accountService;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public UserManagerPanle(Composite parent, int style) {
		super(parent, style);
		this.panle = parent;
		isAddUser = true;
		accountService = new AccountService();
		this.setBounds(0, 0, 387, 345);
		
		CLabel userNameLabel = new CLabel(this, SWT.NONE);
		userNameLabel.setAlignment(SWT.RIGHT);
		userNameLabel.setBounds(10, 10, 42, 21);
		userNameLabel.setText("用户名");
		
		account = new Text(this, SWT.BORDER);
		account.setBounds(58, 10, 131, 21);
		
		CLabel passwordLabel = new CLabel(this, SWT.NONE);
		passwordLabel.setAlignment(SWT.RIGHT);
		passwordLabel.setBounds(198, 10, 42, 21);
		passwordLabel.setText("密码");
		
		password = new Text(this, SWT.BORDER | SWT.PASSWORD);
		password.setBounds(246, 10, 131, 21);
		
		CLabel nameLabel = new CLabel(this, SWT.NONE);
		nameLabel.setAlignment(SWT.RIGHT);
		nameLabel.setBounds(10, 37, 42, 21);
		nameLabel.setText("姓名");
		
		addUserBtn = new Button(this, SWT.NONE);
		addUserBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(!isAddUser) { //修改状态
					try {
						AccountVO accountVo = new AccountVO();
						accountVo.setId(id);
						accountVo.setName(name.getText());
						
						if(isAdmin.getText().equals("是")){
							accountVo.setAdmin(true);
						} else {
							accountVo.setAdmin(false);
						}
						
						//修改用户数据
						accountService.update(accountVo);
						
						//初始化表单
						account.setEditable(true);
						password.setEditable(true);
						
						//修改按钮为“修改用户”
						addUserBtn.setText("增加用户");
						name.setText("");
						isAdmin.select(0);
						
						//刷新表格数据
						refreshTable();
					} catch (MyException e1) {
						MyDialog.alert(panle.getShell(), e1.getMessage());
					}
				} else { //新增状态
					
				}
			}
		});
		addUserBtn.setBounds(297, 64, 80, 25);
		addUserBtn.setText("增加用户");
		
		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(10, 95, 367, 240);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		tableColumn_1 = new TableColumn(table, SWT.NONE);
		tableColumn_1.setWidth(130);
		tableColumn_1.setText("用户名");
		
		tableColumn_2 = new TableColumn(table, SWT.NONE);
		tableColumn_2.setWidth(130);
		tableColumn_2.setText("姓名");
		
		tableColumn_3 = new TableColumn(table, SWT.NONE);
		tableColumn_3.setWidth(100);
		tableColumn_3.setText("是否管理员");
		
		name = new Text(this, SWT.BORDER);
		name.setBounds(58, 37, 131, 21);
		
		adminLabel = new CLabel(this, SWT.NONE);
		adminLabel.setAlignment(SWT.RIGHT);
		adminLabel.setBounds(198, 37, 42, 21);
		adminLabel.setText("管理员");
		
		isAdmin = new Combo(this, SWT.READ_ONLY);
		isAdmin.setBounds(246, 37, 131, 21);
		isAdmin.setItems(new String[]{"否","是"});
		isAdmin.select(0);
		
		table.addMouseListener(new MouseAdapter() {
			// 添加右键菜单
			@Override
			public void mouseDown(MouseEvent e) {
				if (table.getItemCount() == 0) {
					return;
				}
				
				if (e.button == 3) { // 右键
					Menu menu = new Menu(table);
					table.setMenu(menu);
					
					MenuItem updateUserItem = new MenuItem(menu, SWT.PUSH);
					updateUserItem.setText("修改用户");
					
					InputStream is = this.getClass().getResourceAsStream("images/update.png");
					updateUserItem.setImage(new Image(Display.getDefault(), is));
					
					updateUserItem.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent e) {
							isAddUser = false;
							
							//设置用户名和密码输入框为只读
							account.setEditable(false);
							password.setEditable(false);
							
							//修改按钮为“修改用户”
							addUserBtn.setText("修改用户");
							
							TableItem selectItem = table.getSelection()[0];
							
							id = (Integer)selectItem.getData();
							name.setText(selectItem.getText(1));
							
							for(int i = 0; i < isAdmin.getItemCount(); i++){
								if(isAdmin.getItem(i).equals(selectItem.getText(2))) {
									isAdmin.select(i);
								}
							}
						}
					});
					
					MenuItem delUserItem = new MenuItem(menu, SWT.PUSH);
					delUserItem.setText("删除用户");
					
					is = this.getClass().getResourceAsStream("images/del.png");
					delUserItem.setImage(new Image(Display.getDefault(), is));
					
					delUserItem.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent e) {
							
						}
					});
					
					MenuItem resetPasswordItem = new MenuItem(menu, SWT.PUSH);
					resetPasswordItem.setText("重设密码");
					
					is = this.getClass().getResourceAsStream("images/reset.png");
					resetPasswordItem.setImage(new Image(Display.getDefault(), is));
					
					resetPasswordItem.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent e) {
							
						}
					});
				}
			}
		});
		
		refreshTable();
	}
	
	/**
	 * 
	 * 方法概述：刷新表格
	 * 描述：刷新表格
	 * 返回类型：void
	 * 修改人：梁铭显 
	 * 修改时间：2014-5-26
	 */
	public void refreshTable() {
		table.removeAll();
		
		//获取用户数据
		List<AccountVO> accounts = new ArrayList<AccountVO>();
				
		try {
			accounts = accountService.findAll();
		} catch (MyException e) {
			MyDialog.alert(panle.getShell(), e.getMessage());
		}
		
		//填充表格数据
		for(AccountVO account : accounts) {
			TableItem item=new TableItem(table,SWT.NONE);
			item.setText(new String[]{account.getAccount(),account.getName(),account.isAdmin() ? "是" : "否"});
			item.setData(account.getId());
		}
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
